﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.ObjectBuilder;
using NetDon.OA.Business;
using NetDon.OA.Data.Linq;
using NetDon.OA.Interface.Common;

namespace NetDon.OA.Portal.Admin
{
    public partial class VehicleModelEdit : PageBase, IVehicleModelEditView
    {
        private VehicleModelEditPresenter _presenter;

        [CreateNew]
        public VehicleModelEditPresenter Presenter
        {
            set
            {
                _presenter = value;
                _presenter.View = this;
            }
        }

        #region ICurrencyEditView Members

        public Guid VehicleModelID
        {
            get { return Parameters.GetGuid("OID"); }
        }

        public void PopulateVehicleModelData(VehicleModelData vehicleModelData)
        {
            formCurrency.DataSource = vehicleModelData;
            formCurrency.DataBind();
        }

        public void ExtractVehicleModelData(VehicleModelData vehicleModelData)
        {
            formCurrency.ExtractValues(vehicleModelData, false);
            vehicleModelData.AreaId = (CurrentApplicationContext as OApplicationContext).CompanyId;
        }

        public event EventHandler SaveVehicleModel;

        #endregion

        protected virtual void OnSaveVehicleModel(EventArgs e)
        {
            if (SaveVehicleModel != null)
            {
                SaveVehicleModel(this, e);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                _presenter.OnViewInitialized();
            }
            _presenter.OnViewLoaded();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var metaData = new LinqMetaData();
            var count = (from entity in metaData.VehicleModel
                         where entity.ModelName == txtVehicleModelName.Text.Trim()
                         select entity.Id).Count();
            bool duplciateNameExists = VehicleModelID == Guid.Empty ? count > 0 : count > 1;
            if (duplciateNameExists)
            {
                Alert("此车型已存在，请更换名称.");
                return;
            }
            OnSaveVehicleModel(EventArgs.Empty);
        }

  
    }
}